import os
from datetime import datetime

import logzero
import requests
from logzero import logger
from lxml import html
from tqdm import tqdm

from config import COLLECTION_FEED
from utils import get_mongo_collection, record_run_status

pwd = os.path.dirname(os.path.abspath(__file__))
current_module = os.path.splitext(os.path.basename(__file__))[0]
COLLECTION_NAME = current_module
logzero.logfile(f"/tmp/{current_module}.log", maxBytes=30_000_000, backupCount=3, encoding='utf-8')

db = get_mongo_collection(COLLECTION_NAME)

insert_count = 1


@record_run_status(current_module)
def spider(mids):
    for mid in tqdm(mids):
        p = 1
        stop = False
        while not stop:
            index_url = f'https://space.bilibili.com/ajax/member/getSubmitVideos?mid={mid}&pagesize=100&tid=0&page={p}&keyword=&order=pubdate'
            resp = requests.get(index_url).json()
            vlist = resp['data']['vlist']
            if not vlist:
                logger.info('翻页结束')
                break
            for data in vlist:
                data['pic'] = 'http:' + data['pic']
                stop = store_data(data)
                if stop: break
            p += 1
            logger.info('翻页')
    logger.info('完成')
    return insert_count


def get_feeds():
    db_feed = get_mongo_collection(COLLECTION_FEED)
    cursor = db_feed.find({'purpose': 'B站UP主'})
    feeds = []
    for cu in cursor:
        feeds.append(cu['mid'])
    return feeds


def get_detail(aid):
    result = []
    url = f'https://m.bilibili.com/video/av{aid}.html'
    resp = requests.get(url).content
    root = html.fromstring(resp)
    author_img_ele = root.cssselect(
        'div.index__upInfo__M-videoPage-upInfo- > a.index__upFace__M-videoPage-upInfo- > img')
    if len(author_img_ele) > 0:
        author_img = 'http:' + author_img_ele[0].attrib['src']
    else:
        author_img = ''
    '''
        接口修改
    '''
    tags=requests.get(f'https://api.bilibili.com/x/tag/archive/tags?aid={aid}').json()
    if tags['data']:
        for i in tags['data']:
            result.append(i['tag_name'])
    else:
        result=[]
    # tags = root.cssselect('a.index__tag__M-videoPage-relativeTag-')
    # for tag in tags:
    #     t = tag.text
    #     result.append(t)
    return author_img, result


def store_data(data):
    global insert_count
    stop = False
    aid = data['aid']
    old_record = db.find_one({'aid': aid})
    if old_record:
        logger.info(f'{aid} 已经存在，不再爬取')
        stop = True
    else:
        logger.info(f'{aid} 不存在，现在插入')
        data['author_img'], data['tags'] = get_detail(aid)
        data['update_time'] = datetime.now()
        logger.info(data)
        db.insert_one(data)
        insert_count += 1
    return stop


def main():
    feeds = get_feeds()
    spider(feeds)


if __name__ == '__main__':
    main()
